package com.zzxx.controller;

import com.zzxx.entity.*;
import com.zzxx.exceptions.ManagerLoginException;
import com.zzxx.exceptions.ManagerRegisterException;
import com.zzxx.service.ManagerService;
import com.zzxx.utils.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("manage")
public class ManagerController {

	@Autowired(required = false)
	HttpServletRequest request;

	@Autowired
	ManagerService managerService;
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(@RequestBody Map<String,Object> map) throws Exception {
		String managerName = (String) map.get("managerName");
		String password = (String) map.get("password");
		//将密码加密
		password = EncryptUtil.md5Encode(password);
		try {
			Manager manager = managerService.login(managerName, password);
			request.getSession().setAttribute("loginManager",manager);
		} catch (ManagerLoginException e) {
			return e.getMessage();
		}
		return "登陆成功!";
	}

	@RequestMapping(value = "register",method = RequestMethod.POST)
	public String register(@RequestBody HashMap<String, Manager> map) throws Exception {
		Manager manager = map.get("registerManager");
		manager.setPassword(EncryptUtil.md5Encode(manager.getPassword()));
		try {
			managerService.register(manager);
		} catch (ManagerRegisterException e) {
			return e.getMessage();
		}
		return "注册成功!";
	}

	@RequestMapping(value = "chackLogin", method = RequestMethod.GET)
	public Manager checkLogin(){
		Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
		return loginManager;
	}

	@RequestMapping(value = "hexiaoList/{mid}", method = RequestMethod.GET)
	public List<ManagerQr> getWriteOffList(@PathVariable("mid") String managerId){
		Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
		if (managerId==null||loginManager==null||!managerId.equals(loginManager.getId())) return null;
		else return managerService.getWriteOffList(managerId);
	}

	@RequestMapping(value = "myGymList", method = RequestMethod.GET)
	public List<GymAndPower> findGymList(){
		Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
		if (loginManager==null) return null;
		List<GymAndPower> list = managerService.findGymListById(loginManager.getId());
		return list;
	}

	@RequestMapping(value = "findGap/{id}", method = RequestMethod.GET)
	public GymAndPower findGapById(@PathVariable("id") String id){
		GymAndPower gap = managerService.findGapById(id);
		return gap;
	}

	@RequestMapping(value = "myActivityList", method = RequestMethod.GET)
	public List<GymAndPower> findActivityList(){
		Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
		if (loginManager==null) return null;
		List<GymAndPower> list = managerService.findActivityListById(loginManager.getId());
		return list;
	}

	@RequestMapping(value = "findAmp/{id}", method = RequestMethod.GET)
	public GymAndPower findAmpById(@PathVariable("id") String id){
		GymAndPower amp = managerService.findAmpById(id);
		return amp;
	}

	@RequestMapping(value = "signOut",method = RequestMethod.GET)
	public String signOut(){
		request.getSession().setAttribute("loginManager",null);
		return "success";
	}
}
